//
//  HOReportViewController.m
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOReportViewController.h"
#import "HOUtils.h"
#import <AVFoundation/AVFoundation.h>
#import "HOUtils.h"
#import "HORecordView.h"
#import "HORecordActiveView.h"
#import "HODefine.h"
#import "VoiceConverter.h"
#import "HODepartmentViewController.h"
#import "HOFaultCategoryViewController.h"
#import "HOHopeHandlerViewController.h"
#import "HOReportLocationViewController.h"
#import "HOConstants.h"
#import "HOUserInfoEntity.h"
#import "HOUIMgr.h"
#import "HOStaticDataMgr.h"
#import "HOAreaEntity.h"
#import "HOAssignViewController.h"
#import "HOHopeHanderEntity.h"
#import "HOHybridTypeViewController.h"


@interface HOReportViewController()<HOReportLocationViewControllerDelegate, HOAssignViewControllerDelegate>
{
	
}
@property(nonatomic, retain)HOUserInfoEntity *userInfo;
//@property(nonatomic, retain)HOAreaEntity *areaEntity;
@end

@implementation HOReportViewController

- (void)viewDidLoad
{
	[super viewDidLoad];
	[self initTableView];
	[self initSubtViews];
	[self initMediaView];
	[self registerTapGesture];
	[self initDataSource];
	[self requestUserInfo];
	[self registerKeyboard];
//	[self initReportDepartment];
	[self initReportAreaFromLocal];
}

- (void)initDataSource
{
	self.userInfo = [[HOStaticDataMgr sharedInstance] getUserInfo];
}

- (void)requestUserInfo
{
	if(!self.userInfo.isGotData)
		[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_GetUserInfo}];
}

- (void)initSubtViews
{

	baseTableview.tableFooterView = [self getFooterViewWithTitle:@"提交"];
	//	baseTableview.separatorStyle = UITableViewCellSelectionStyleGray;
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	[self hideKeyboard];
}

- (void)didClickFooterButton:(UIButton *)btn
{
	if(![self isSelectFaultType:btn])
		return;

	
//	NSString *faultDetail = [self getTrimingCharaFromTextView:[self.reportDescEditView getTextView]];
//	if(faultDetail == nil || faultDetail.length == 0)
//	{
//		[self popupHintView:NSLocalizedString(@"请填写故障情况描述！", @"请填写故障情况描述！")];
//		return;
//	}
	NSString *areaIds = [self getSelectArea];
	if(areaIds.length == 0)
	{
		[self popupHintView:@"请选择报修地点!"];
		return;
	}
	NSString *typeIds = [self getSelectTypeIds];
	if(typeIds.length == 0)
	{
		[self popupHintView:@"请选择报修类型!"];
		return;
	}
	
	[self convertingVoice];
}

- (NSDictionary *)getReportDic
{
	NSString *faultDetail = [self getTrimingCharaFromTextView:[self.reportDescEditView getTextView]];
	if(faultDetail == nil || faultDetail.length == 0)
	{
		faultDetail = @"无";
//		[self popupHintView:NSLocalizedString(@"请填写故障情况描述！", @"请填写故障情况描述！")];
//		return;
	}
	NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:[super getMediaDic]];
	[dic setObject:kHO_ACTION_AddNormalReport forKey:kHO_FIELD_Action];
	[dic setObject:faultDetail forKey:kHO_FIELD_FaultDetail];
	

	NSString *areaIds = [self getSelectArea];	[dic setObject:areaIds forKey:kHO_FIELD_AreaId];
	[dic setObject:[NSNumber numberWithInt:self.selectedDepartmentEntity.departmentId] forKey:kHO_FIELD_DepartmentId];
	if(self.hopeHandler)
	{
		[dic setObject:[NSNumber numberWithInt:self.hopeHandler.userId] forKey:kHO_FIELD_HopeId];
	}
	[dic setObject:[self getSelectTypeIds] forKey:kHO_FIELD_FaultId];
	
	return dic;
}

- (void)submit
{
	[super submit];
	NSDictionary *dic = [self getReportDic];
	if(dic == nil)
		return;
	self.view.userInteractionEnabled = NO;
	[self sendRequestWithParams:dic];
}

- (void)onKeyboardWillShow:(NSNotification *)notification
{
	[super onKeyboardWillShow:notification];
	[baseTableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:2] atScrollPosition:UITableViewScrollPositionTop animated:YES];
}
- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
	[self hideKeyboard];
}

#pragma mark --

- (void)viewWillAppear:(BOOL)animated
{
	self.tabBarController.tabBar.hidden = !self.needTabBar;
	self.navigationItem.hidesBackButton = self.needTabBar;
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:self.navTitle];
	[self hideKeyboard];
	[self resetTableViewFrame];
	[self displayRightButtonItemWithTitle:NSLocalizedString(@"重置", @"重置")];
	[baseTableview reloadData];
}


	
	


- (void)rightButtonItemDidClick
{
	[self cleanup];
}

- (void)dealloc
{
	self.userInfo = nil;
	[super dealloc];
}

#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0)
		return 44;
	else if(indexPath.section == 1)
	{
		NSString *content = [self getSelectTypeNames];
		CGSize contentSize = [HOUtils findHeightForText:content
											havingWidth:tableView.frame.size.width - 120
												andFont:[UIFont systemFontOfSize:13]];
		CGFloat height = MAX(22, contentSize.height);
		return height + 25;
	}
	else if(indexPath.section == 2)
	{
		if(indexPath.row == 0)
		{
			return [self getPhotoAndRecordViewHeight];
		}
		else
		{
			return 58 + 128 + 22;
		}
	}
	return 0;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if(section == 0)
		return 4;
	else if(section == 1)
		return 1;
	else if(section == 2)
		return 2;
	return 0;
}

- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	if(section == 0)
		return 0.0001;
	else if(section == 1
			|| section == 2)
		return SECTION_PADDING / 2;
	return 0;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0 || indexPath.section == 1)
	{
		static NSString *HOReportViewCellIdentifier = @"HOReportViewCellIdentifier";
		HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:HOReportViewCellIdentifier];
		if(cell == nil)
		{
			cell = [[[HOTableViewCell alloc]
					 initWithStyle:UITableViewCellStyleDefault
					 reuseIdentifier: HOReportViewCellIdentifier] autorelease];
		}
		CGFloat cellHeight = [self tableView:tableView heightForRowAtIndexPath:indexPath];
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
		cell.textLabel.textColor = TEXT_COLOR_181818;
		cell.textLabel.font = [UIFont systemFontOfSize:15];
		UILabel *detailLbl = (UILabel *)[cell viewWithTag:114];
		if(detailLbl == nil)
		{
			detailLbl = [[[UILabel alloc]
						  initWithFrame:CGRectMake(100,
												   (cellHeight - 22) / 2,
												   tableView.frame.size.width - 120,
												   22)] autorelease];
			detailLbl.tag = 114;
			detailLbl.font = [UIFont systemFontOfSize:15];
			detailLbl.textColor = TEXT_COLOR_666666;
			[cell.contentView addSubview:detailLbl];
		}
		detailLbl.hidden = YES;
		detailLbl.numberOfLines = 1;
		if(indexPath.section == 0)
		{
			detailLbl.hidden = NO;
			if(indexPath.row == 0)
			{
				cell.textLabel.text = NSLocalizedString(@"报修人", @"报修人");
				detailLbl.text = self.userInfo.realName;
			}
			else if(indexPath.row == 1)
			{
				cell.textLabel.text = NSLocalizedString(@"报修科室", @"报修科室");
				detailLbl.text = self.userInfo.departmentName;
			}
			else if(indexPath.row == 3)
			{
				cell.textLabel.text = NSLocalizedString(@"报修地点", @"报修地点");
				detailLbl.numberOfLines = 2;
				detailLbl.text = [self getSelectAreaName];
				[detailLbl sizeToFit];
				detailLbl.frame = CGRectMake(100,
											 (cellHeight - detailLbl.frame.size.height) / 2,
											 tableView.frame.size.width - 120,
											 detailLbl.frame.size.height);
//				if(self.selectedSecondAreaEntity)
//				{
//					detailLbl.text = self.selectedSecondAreaEntity.areaName;
//				}
//				else
//				{
//					detailLbl.text = @"";
//				}
				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
				cell.selectionStyle = UITableViewCellSelectionStyleGray;
			}
			else if(indexPath.row == 2)
			{
				cell.textLabel.text = NSLocalizedString(@"报修电话", @"报修电话");
				detailLbl.text = self.userInfo.virtualCellPhone;
			}
		}
		if(indexPath.section == 1)
		{
			detailLbl.hidden = NO;
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			cell.selectionStyle = UITableViewCellSelectionStyleDefault;
			detailLbl.numberOfLines = 3;
			if(indexPath.row == 0)
			{
				cell.textLabel.text = NSLocalizedString(@"故障分类", @"故障分类");
				
				detailLbl.text = [self getSelectTypeNames];
				detailLbl.frame = CGRectMake(90,
											 (cellHeight - detailLbl.frame.size.height) / 2,
											 tableView.frame.size.width - 120,
											 100);
				detailLbl.numberOfLines = 3;
				[detailLbl sizeToFit];
				detailLbl.frame = CGRectMake(90,
											 (cellHeight - detailLbl.frame.size.height) / 2,
											 tableView.frame.size.width - 120,
											 detailLbl.frame.size.height);
			}
//			else if(indexPath.row == 2)
//			{
//				cell.textLabel.text = NSLocalizedString(@"希望受理人", @"希望受理人");
//				if(self.hopeHandler)
//				{
//					detailLbl.text = self.hopeHandler.userName;
//				}
//				else
//				{
//					detailLbl.text = @"";
//				}
//				cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//				cell.selectionStyle = UITableViewCellSelectionStyleGray;
//			}
		}
		return cell;
	}
	else if(indexPath.section == 2)
	{
		return [self tableView:tableView cellForTakePhotoRowAtIndexPath:indexPath];
	}
	return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
	if(indexPath.section == 0)
	{
		if(indexPath.row == 3)
		{
			
			HOReportLocationViewController *viewCtrl = [[HOReportLocationViewController alloc] init];
			viewCtrl.delegate = self;
//			[viewCtrl initSearchBar];
			viewCtrl.selectedFirstAreaEntity = self.selectedFirstAreaEntity;
			viewCtrl.selectedSecondAreaEntity = self.selectedSecondAreaEntity;
			viewCtrl.selectedThirdAreaEntity = self.selectedThirdAreaEntity;
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
		}
	}
	else if(indexPath.section == 1)
	{
		if(indexPath.row == 2)
		{
			if(self.selectedDepartmentEntity == nil)
			{
				[self popupHintView:NSLocalizedString(@"请先选择负责部门！", @"请先选择负责部门！")];
				return;
			}
			HOAssignViewController *viewCtrl = [[HOAssignViewController alloc] init];
			viewCtrl.delegate = self;
			viewCtrl.departmentId = self.selectedDepartmentEntity.departmentId;
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
			
		}
		else
		{
			HOHybridTypeViewController *viewCtrl = [[HOHybridTypeViewController alloc] init];
			viewCtrl.delegate = self;
			viewCtrl.currentEntity = self.selectedFaultGategoryEntity;
			viewCtrl.subEntity = self.selectedSubFaultGategoryEntity;
			viewCtrl.currentDepartmentEntity = self.selectedDepartmentEntity;
			[self.navigationController pushViewController:viewCtrl animated:YES];
			[viewCtrl release];
		}
	}
}


- (void)didSelectRepairer:(HORepairerEntity *)entity;
{
	self.hopeHandler = entity;
	[baseTableview reloadData];
}


- (void)cleanup
{
	self.hopeHandler = nil;
	[super cleanup];
	[self hideKeyboard];
	[baseTableview reloadData];
}
#pragma mark

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_GetUserInfo])
	{
		NSDictionary *dic = response.dataNode;
		[self.userInfo parseDic:dic];
		[baseTableview reloadData];
	}
	else if([response isEqualAction:kHO_ACTION_AddNormalReport])
	{
		[self didSubmitReport];
		[self cleanup];
		[self sendCommand:kHO_CMD_Report_List_Changed data:nil];
		
		[baseTableview reloadData];
	}
}


@end
